#include "cppTutorDoublyLinkedList.h"

#include <iostream>

using namespace std;

CcppTutorDoublyLinkedList :: CcppTutorDoublyLinkedList ()
{
    head = NULL;
    tail = NULL;
    n    = NULL;
    temp = NULL;
}

CcppTutorDoublyLinkedList :: ~CcppTutorDoublyLinkedList ()
{
}

void CcppTutorDoublyLinkedList :: BuildList ()
{
    // creat a first node
    n = new node;   // creates a new node based on how we define our struct
    n->data = 1;    //plce 1 inside the data of a node
    n->next = NULL; // It is the first node and points to NULL. It will search a node n and make it next to point to NULL
    head    = n;    // this is the first node so head points ot the n;
    tail    = n;    // it is the only node so tail will be pointing to the n only

    // create a second node
    n = new node;   // It is pointing to a brand new node
    n->data = 2;
    n->prev = tail; // second node is poiting to the same the first is pointing and is connected to the first node
    tail->next = n; // tail will point to the same the first node and first is now poiting to the second node
    tail = tail->next;  // tails is still pointing to first so make it to point to the last of second node or end of list

    // create a third node
    n = new node;   // It is pointing to a brand new node
    n->data = 3;
    n->prev = tail;
    tail->next = n;
    tail = tail->next;

    // create a fourth node
    n = new node;   // It is pointing to a brand new node
    n->data = 4;
    n->prev = tail;
    tail->next = n;
    tail = tail->next;

    tail->next = NULL;
    head->prev = NULL;
}

void CcppTutorDoublyLinkedList :: printHeadToTail ()
{
    cout << "Printing head to tail : " << endl;
    temp = head;    // making temp poiting to the first node
    while (temp != NULL) // till it reaches the end
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

void CcppTutorDoublyLinkedList :: printTailToHead ()
{
    cout << "Printing tail to head : " << endl;
    temp = tail; // point it to the end of list
    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->prev;
    }
    cout << endl;
}